package pkg137;

//Client Application
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JOptionPane;

public class Client extends Frame implements ActionListener,Runnable{
    Socket s;
    BufferedReader br;
    BufferedWriter bw;
    TextField text;
    Button button1,button2;
    List list;
    static int portNum;
    static String IP, username;
    
    public static void main(String arg[]){
        getUsername();	
	}
    
    @Override
    public void run(){
        try{
            s.setSoTimeout(1);}catch(Exception e){}
	while (true){
            try{list.addItem(br.readLine());
            }catch (Exception h){}
	}
    }
	
    public static void getUsername(){
        username = JOptionPane.showInputDialog(null, "Enter Username: ","Input",JOptionPane.QUESTION_MESSAGE);
        if(username==null) System.exit(0);
	else getIP();
	}
	
	public static void getIP(){
            IP = JOptionPane.showInputDialog(null, "Enter IP Address: ","Input",JOptionPane.QUESTION_MESSAGE);
            if(IP==null) System.exit(0);
            else getPort();
	}
	
	public static void getPort(){
            int temp = 0;
            do{
                String port = JOptionPane.showInputDialog(null,"Port Number: ","Input Port",JOptionPane.QUESTION_MESSAGE);
                
                if(port==null) System.exit(0);
		else{
                    Pattern p = Pattern.compile("[A-Z,a-z,&%$#@!()*^]");
                    Matcher m = p.matcher(port);
                    
                    if (m.find()){
                        JOptionPane.showMessageDialog (null, "Please enter only number.","Try again",JOptionPane.ERROR_MESSAGE);
                    }
                    else{
                        temp =1;
        		portNum = Integer.parseInt(port);
        		new Client("ClientApp_" +username);
                    }
		}
            }
            while(temp == 0);
	}
	
        public Client(final String m){
            super(m);
            setSize(350,350);
            setLocation(200,200);
            setResizable(false);
            
            this.setLayout(new BorderLayout());
            button1 = new Button("Send");
            button1.setBackground(Color.gray);
            button2 = new Button("Exit");
            button2.setBackground(Color.gray);
            button1.addActionListener(this);
            button2.addActionListener(this);
            list = new List();
            list.setBackground(Color.black);
            list.setForeground(Color.white);
            text = new TextField();
            
            
            text.addKeyListener(new KeyAdapter(){
          
            public void keyPressed(KeyEvent e){
                if(e.getKeyCode()==KeyEvent.VK_ENTER){
                    try{
                    bw.write( username +" said: "+ text.getText());
                    text.setText("");
                    bw.newLine();bw.flush();
                    }catch(Exception m){}
             }  
                else if (e.getKeyCode()==KeyEvent.VK_ESCAPE){
                    System.exit(0);
             }
           }});
            
            add(list,"Center");
            add(button1,"West");
            add(button2,"East");		
            add(text,"South");
            setVisible(true);
	
            try{
                s = new Socket(IP,portNum);
		br = new BufferedReader(new InputStreamReader(s.getInputStream()));
		bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
		
                Thread th;
		th = new Thread(this);
		th.start();
            }catch(Exception e){}	
	}
        
    @Override
    public void actionPerformed ( ActionEvent e){
        if (e.getSource().equals(button2))System.exit(0);
        else{
            try{
                bw.write(text.getText());
		bw.newLine();bw.flush();
            }catch(Exception m){}
           }			  
	}
}